package com.pshcp.service;

import com.framework.util.SystemUtil;
import com.pshcp.domain.dto.LabelDTO;
import com.pshcp.domain.model.Label;
import com.pshcp.mapper.LabelMapper;

import com.github.pagehelper.PageHelper;
import com.framework.base.PageList;
import com.framework.tool.CommonTool;
import com.pshcp.common.enums.CommonStatusEnum;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 标签表 服务实现类
 * </p>
 *
 * @author auto generator
 * @since 2022-11-02
 */
@Service
public class LabelService {

    @Autowired
    private LabelMapper labelMapper;

    public PageList<Label> list(LabelDTO labelDTO){
        if(labelDTO.getPageNum() != null && labelDTO.getPageSize() != null){
            PageHelper.startPage(labelDTO.getPageNum(), labelDTO.getPageSize());
        }
        Example example = new Example(Label.class);
        Example.Criteria criteria = example.createCriteria();
        if(labelDTO.getStatus() != null){
            criteria.andEqualTo("status", labelDTO.getStatus());
        } else {
            criteria.andEqualTo("status", CommonStatusEnum.INIT.getCode());
        }
        if(labelDTO.getType() != null){
            criteria.andEqualTo("type", labelDTO.getType());
        }
        //搜素
        if (!StringUtils.isBlank(labelDTO.getSearch())) {
            Example.Criteria criteria1 = example.createCriteria();
            criteria1.orLike("name", "%" + labelDTO.getSearch() + "%");
            example.and(criteria1);
        }
        example.setOrderByClause(" created_time desc");
        List<Label> list = labelMapper.selectByExample(example);
        return CommonTool.buildPageList(list);
    }

    public List<Label> select(Label label){
        return labelMapper.select(label);
    }

    public int insert(Label label){
        label.setId(SystemUtil.getId());
        label.setStatus(CommonStatusEnum.INIT.getCode());
        label.setCreatedTime(new Date());
        return labelMapper.insert(label);
    }

    public int update(Label label){
        label.setUpdateTime(new Date());
        return labelMapper.updateByPrimaryKeySelective(label);
    }

    public Label findById(Long id){
        return labelMapper.selectByPrimaryKey(id);
    }

    public List<Label> findByIds(List<Long> ids){
        Example example = new Example(Label.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andIn("id", ids);
        return labelMapper.selectByExample(example);
    }

    public int delById(Long id){
        Example example = new Example(Label.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("id", id);

        Label label = new Label();
        label.setStatus(CommonStatusEnum.DEL.getCode());
        return labelMapper.updateByExampleSelective(label, example);
    }

}
